package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.plugin.job.type.simple.AbstractSimpleElasticJob;
import com.lsh.oms.core.email.SendEmailService;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.redis.RedisTemplate;
import com.lsh.oms.core.service.finBill.FinBillService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Slf4j
@Component
@Deprecated
public class BillCreateKafkaJop extends AbstractSimpleElasticJob {

    public static String billKey = "oms:bill:push:kafka:time";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FinBillService finBillService;

    @Autowired
    private SendEmailService sendEmailService;

    private int timePoint = 20;

    @Override
    public void process(JobExecutionMultipleShardingContext jobExecutionMultipleShardingContext) {

        try {
            Map<Integer, String> shardItems = jobExecutionMultipleShardingContext.getShardingItemParameters();

            if (shardItems.isEmpty()) {
                return;
            }

            this.init(shardItems);

            log.info("[shardItems is ({})], [timePoint is ({})]", JSON.toJSONString(shardItems), timePoint);
            Integer startTime = this.getStartTime();
            Integer endTime = this.getEndTime(startTime, timePoint * 60);

            int nowTime = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
            if (nowTime < endTime) {
                log.info("结束时间大于当前时间,任务暂停一次");
                return;
            }

            log.info("【Bill下单】【统计范围】 ({} - {}) ", startTime, endTime);
            log.info("【处理签收单task】 【开始】");
            this.finBillService.addSignTask(startTime, endTime,TaskType.Bill_SIGN_BY_MQ);
            log.info("【处理签收单task】 【结束】");
            log.info("【处理反仓单task】 【开始】");
            this.finBillService.addRoTask(startTime, endTime, TaskType.Bill_RO_BY_MQ);
            log.info("【处理反仓单task】 【结束】");
            log.info("redis 最新处理时间 {}", endTime);
            this.redisTemplate.set(billKey, String.valueOf(endTime));
        } catch (Exception ex) {

            String message = ex.getMessage();
            if (message.length() > 100) {
                message = message.substring(0, 100);
            }

            this.sendEmailService.asyncSendEmail(message, "创建Bill异常");

            log.error("[omsTask 添加] 异常", ex);
        }

    }

    private void init(Map<Integer, String> shardItems) {

        Collection<String> timeItems = shardItems.values();
        Iterator<String> iteratorItems = timeItems.iterator();
        String timeItem = null;
        if (iteratorItems.hasNext()) {

            timeItem = iteratorItems.next();
        }

        if (StringUtils.isNotBlank(timeItem)) {
            timePoint = Integer.valueOf(timeItem);
        }
    }


    /**
     * 统计开始时间
     *
     * @return 时间戳
     */
    private Integer getStartTime() {

        String time = redisTemplate.get(billKey);
        int transTime;
        if (StringUtils.isBlank(time)) {

            transTime = 1570636800;

            redisTemplate.set(billKey, String.valueOf(transTime));
        } else {
            transTime = Integer.valueOf(time);
        }

        return transTime;
    }

    /**
     * 统计结束时间
     *
     * @param timePoint 时间点
     * @return 时间戳
     */
    private Integer getEndTime(int startTime, int timePoint) {

        return startTime + timePoint;
    }
}
